/**
 * Copyright (C) 2011  Robin Dietrich, Nils Kenneweg, Leonard Stutz
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 **/
 
#ifndef BASEAPPLICATION_HPP
#define BASEAPPLICATION_HPP

#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <Sample.h>

#include <OISEvents.h>
#include <OISInputManager.h>
#include <OISKeyboard.h>
#include <OISMouse.h>

#include <SdkTrays.h>
#include "CameraMan.h"

#include <CEGUI/CEGUI.h>
#include <CEGUI/RendererModules/Ogre/CEGUIOgreRenderer.h>

namespace minecraft {
	class BaseApplication : public Ogre::FrameListener, public Ogre::WindowEventListener, public OIS::KeyListener, public OIS::MouseListener, OgreBites::SdkTrayListener {
		public:
			BaseApplication(void);
			virtual ~BaseApplication(void);

			virtual void go(void);
            
            bool quitButtonEvtY(const CEGUI::EventArgs &e);
            bool quitButtonEvtN(const CEGUI::EventArgs &e);

		protected:
			virtual bool setup();
			virtual bool configure(void);
			virtual void chooseSceneManager(void);
			virtual void createCamera(void);
			virtual void createFrameListener(void);
			virtual void createScene(void) = 0; // Override me!
			virtual void destroyScene(void);
			virtual void createViewports(void);
			virtual void setupResources(void);
			virtual void createResourceListener(void);
			virtual void loadResources(void);

			// Ogre::FrameListener
			virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);
            CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID);            
            
			// OIS::KeyListener
			virtual bool keyPressed( const OIS::KeyEvent &arg );
			virtual bool keyReleased( const OIS::KeyEvent &arg );
			// OIS::MouseListener
			virtual bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id );

			// Ogre::WindowEventListener
			//Adjust mouse clipping area
			virtual void windowResized(Ogre::RenderWindow* rw);
			//Unattach OIS before window shutdown (very important under Linux)
			virtual void windowClosed(Ogre::RenderWindow* rw);

			Ogre::Root *mRoot;
			Ogre::Camera* mCamera;
			Ogre::SceneManager* mSceneMgr;
			Ogre::RenderWindow* mWindow;
            CEGUI::OgreRenderer* mRenderer;
			Ogre::String mResourcesCfg;
			Ogre::String mPluginsCfg;

			// OgreBites
			OgreBites::SdkTrayManager* mTrayMgr;
			CameraMan* mCameraMan;       // basic camera controller
			OgreBites::ParamsPanel* mDetailsPanel;     // sample details panel
			bool mCursorWasVisible;                    // was cursor visible before dialog appeared
			bool mShutDown;

			//OIS Input devices
			OIS::InputManager* mInputManager;
			OIS::Mouse*    mMouse;
			OIS::Keyboard* mKeyboard;
            
            bool quit_state; //quit state
            bool cegui_state; //cegui state
			bool game_started; //game started
	};
}

#endif // #ifndef BASEAPPLICATION_HPP
